package com.bittech.servlet.smallfileserver.web.view;

import com.bittech.servlet.smallfileserver.web.config.listener.FreemarkerConfigLoaderListener;
import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author: secondriver
 * Created: 2018/8/27
 */
public class FileViewBaseServlet extends HttpServlet {
    
    /**
     * 获取模板
     *
     * @param templateName
     * @param request
     * @param response
     * @return
     */
    protected Template getTemplate(String templateName, HttpServletRequest request, HttpServletResponse response) {
        ServletContext servletContext = request.getServletContext();
        Configuration configuration =
                (Configuration) servletContext.getAttribute(FreemarkerConfigLoaderListener.TEMPLATE_KEY);
        try {
            return configuration.getTemplate(templateName);
        } catch (IOException e) {
            return null;
        }
    }
}
